package cn.jiyun.service.impl;

import cn.jiyun.fegin.VipFegin;
import cn.jiyun.mapper.DianPuMapper;
import cn.jiyun.pojo.DianPu;
import cn.jiyun.pojo.Vip;
import cn.jiyun.service.DianPuService;
import cn.jiyun.vo.ResultCommon;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DianPuServiceImpl implements DianPuService {

    @Autowired
    private DianPuMapper dianPuMapper;

    @Autowired
    private VipFegin vipFegin;

    @Override
    public Integer add(DianPu dianPu) {
        return dianPuMapper.add(dianPu);
    }

    @Override
    public void delete(Integer id) {
        dianPuMapper.delete(id);
    }

    @Override
    public PageInfo<DianPu> findAll(Integer pageNo, Integer pageSize, DianPu dianPu) {
        PageHelper.startPage(pageNo,pageSize);

        List<DianPu> dianPuList  = dianPuMapper.findAll();
        List<Vip> vipList = vipFegin.findAll();

        Map<Integer,Vip> vipMap = new HashMap<Integer,Vip>();
        vipList.forEach(vip -> {
            vipMap.put(vip.getId(),vip);
        });
        dianPuList.forEach(dianpu -> {
            dianpu.setVname(vipMap.get(dianpu.getVid()).getName());
        });

        PageInfo<DianPu> pageInfo = new PageInfo<>(dianPuList);
        return pageInfo;
    }


}
